package com.xerland.tbootstrap.model;

import java.util.ArrayList;
import java.util.List;
import java.util.ListIterator;

public class MenuBean {
    public static final int RtdListSize = 10;
    private int mIconResId;
    private String mMachName;
    private  String mPowerOnTime;
    private String mPowerOffTime;
    private int mMachID;
    private String mSaveTime;
    private double mSetTemp;
    private double mActTemp;
    private double mSetHumi;
    private double mActHumi;
    private List<RealTimeData> realTimeDataList;

    public MenuBean() {
        this.realTimeDataList = new ArrayList<RealTimeData>();
    }

    public MenuBean(int mIconResId,
                    String mMachName,
                    String mPowerOnTime,
                    String mPowerOffTime,
                    int mMachID,
                    String mSaveTime,
                    double mSetTemp,
                    double mActTemp,
                    double mSetHumi,
                    double mActHumi) {
        this.mIconResId = mIconResId;
        this.mMachName = mMachName;
        this.mPowerOnTime = mPowerOnTime;
        this.mPowerOffTime = mPowerOffTime;
        this.mMachID = mMachID;
        this.mSaveTime = mSaveTime;
        this.mSetTemp = mSetTemp;
        this.mActTemp = mActTemp;
        this.mSetHumi = mSetHumi;
        this.mActHumi = mActHumi;
        this.realTimeDataList = new ArrayList<RealTimeData>();
    }

    public String getMachName() {
        return mMachName;
    }

    public String getTimeBegin() {
        return mPowerOnTime;
    }

    public String getTimeEnd() {
        return mPowerOffTime;
    }

    public MenuBean setIconResId(int mIconResId) {
        this.mIconResId = mIconResId;
        return this;
    }

    public MenuBean setMachName(String mMachName) {
        this.mMachName = mMachName;
        return this;
    }

    public MenuBean setTimeBegin(String mPowerOnTime) {
        this.mPowerOnTime = mPowerOnTime;
        return this;
    }

    public MenuBean setTimeEnd(String mPowerOffTime) {
        this.mPowerOffTime = mPowerOffTime;
        return this;
    }

    public int getIconResId() {
        return mIconResId;
    }

    public int getMachID() {
        return mMachID;
    }

    public String getSaveTime() {
        return mSaveTime;
    }

    public double getSetTemp() {
        return mSetTemp;
    }

    public double getActTemp() {
        return mActTemp;
    }

    public double getSetHumi() {
        return mSetHumi;
    }

    public double getActHumi() {
        return mActHumi;
    }

    public int getState() {
        if (this.mPowerOnTime.isEmpty()) {
            return PowerStateBean.POWER_STATE_ABNORMAL;
        }
        PowerStateBean powerStateBean = new PowerStateBean(this.mMachID, this.mMachName, this.mPowerOnTime, this.mPowerOffTime);
        return powerStateBean.getState();
    }

    public List<RealTimeData> getRealTimeDataList() {
        List<RealTimeData>  realTimeDataListT = new ArrayList<RealTimeData>();
        ListIterator<RealTimeData> iterator=realTimeDataList.listIterator(realTimeDataList.size());
        while(iterator.hasPrevious()){
            realTimeDataListT.add((iterator.previous()));
        }
        return realTimeDataListT;
    }

    public void setExploreBeanList(List<RealTimeData> realTimeDataList) {
        this.realTimeDataList = realTimeDataList;
    }

    public void addRtdBeanList(RealTimeData realTimeData) {
        if (realTimeDataList.size() >= RtdListSize) {
            realTimeDataList.remove(0);
        }
        realTimeDataList.add(realTimeData);
        this.mMachID = realTimeData.getMachID();
        this.mSaveTime = realTimeData.getSaveTime();
        this.mSetTemp = realTimeData.getSetTemp();
        this.mActTemp = realTimeData.getActTemp();
        this.mSetHumi = realTimeData.getSetHumi();
        this.mActHumi = realTimeData.getmActHumi();
        syncStateWithRtd(realTimeData);
    }

    public void updateWithPower(PowerStateBean powerStateBean) {
        this.mMachID = powerStateBean.getMachID();
        this.mMachName = powerStateBean.getMachName();
        this.mPowerOnTime = powerStateBean.getPowerOnTime();
        if( powerStateBean.getState() != PowerStateBean.POWER_STATE_ABNORMAL){
            this.mPowerOffTime = powerStateBean.getPowerOffTime();
        }
    }

    private void syncStateWithRtd(RealTimeData realTimeData) {
        if (getState() == PowerStateBean.POWER_STATE_ON) {
            return;
        }
        switch (getState()) {
            case PowerStateBean.POWER_STATE_ABNORMAL:   // case of offline to online back
                this.mPowerOffTime = PowerStateBean.NULL_TIME; //NULL_TIME of power_off_time for PowerOn state
                break;
            case PowerStateBean.POWER_STATE_OFF:        // case of no record of PowerOnTime
                this.mPowerOnTime = realTimeData.getSaveTime();
                this.mPowerOffTime = PowerStateBean.NULL_TIME;
                break;
        }
    }

    public String getFirstSaveTimeInList() {
        if (this.realTimeDataList.size() > 0) {
            return this.realTimeDataList.get(0).getSaveTime();
        }
        return null;
    }
    public String toPowerStateBeanJson() {
        return String.format("{\"mMachID\":%d, \"mMachName\":\"%s\", \"mPowerOnTime\":\"%s\", \"mPowerOffTime\":\"%s\"}",
                this.mMachID,
                this.mMachName,
                this.mPowerOnTime,
                this.mPowerOffTime);
    }
}
